$().ready(function () {
    loadAccount();
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/keep/consumptionHistory/save",
        data: $('#signupForm').serialize(),
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            keepConsumptionTypeId: {
                required: true
            }
        },
        messages: {
            keepConsumptionTypeId: {
                required: icon + "请选择消费类型"
            }
        }
    })
}


function loadAccount(){
    var html = "";
    $.ajax({
        url : '/keep/account/myAccount',
        success : function(data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
            }
            $(".chosen-select-account").append(html);
            $(".chosen-select-account").chosen({
                maxHeight : 200
            });
        }
    });
}

//账户
$('.chosen-select-account').change(function() {
    var selected = $(".chosen-select-account").val();
    if (selected === "") {
        $("#consumptionType_div").hide()
        $(".chosen-select-consumptionType").val("");
        $(".chosen-select-consumptionType").attr('disabled','disabled');
    }else {
        $("#consumptionType_div").show()
        $(".chosen-select-consumptionType").removeAttr("disabled");
    }
});

/**
 * 收支类型修改事件
 */
$('.chosen-select-consumptionType').change(function() {
    var selected = $('.chosen-select-consumptionType').val();
    if (selected === "") {
        $("#keepConsumptionTypeId_div").hide()
        $(".chosen-select-keepConsumptionTypeId").val("");
        $(".chosen-select-keepConsumptionTypeId").attr('disabled','disabled');
    }else {
        $("#keepConsumptionTypeId_div").show();
        $(".chosen-select-keepConsumptionTypeId").removeAttr("disabled");
        loadKeepConsumptionTypeId();
    }
});

function loadKeepConsumptionTypeId(){
    var keepAccountId = $(".chosen-select-account").val();
    var consumptionType = $(".chosen-select-consumptionType").val();
    var html = "<option value=''>--请选择消费类型--</option>";
    $.ajax({
        url : '/keep/keepConsumptionType/listByKeepAccountIdAndConsumptionType/'+ keepAccountId +'/' + consumptionType,
        async: false,
        success : function(data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
            }
            $(".chosen-select-keepConsumptionTypeId").html(html);
        }
    });
}

layui.use('laydate', function () {
    var laydate = layui.laydate;
    // 日期时间选择器
    laydate.render({
        elem: '#expensesOrReceiptsTime',
        type: 'datetime'
    });
});
